<?php
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';
require_once 'TVB/ImageInfrastructure/Client/requires.php';

class ImageSwitchAction extends BaseAction
{
    public $id;
    public $ut;

    public function execute()
    {
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_image();
		}
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_image();
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_image();
		}
        elseif (isset($_REQUEST['unpublish']))
        {
            return  $this->unpublish_image();
        }
        elseif (isset($_REQUEST['unpublish_premium']))
        {
            return  $this->unpublish_premium_image();
        }
    }

	/**
	 * recover image
	 */
	private function recover_image()
	{
		try
		{
			$image = new image();
			$image = $image->get($this->id);
			$image->status = AC::STATUS_NEW;
			$update_id = Model::$dbo['w']->save($image);
			if ($update_id > 0) {
				$this->addInfo("image:".AC::LOG_OP_RECOVER.":".$this->id, "image recover success", json_encode($image));

				// remove remote file
				if($image->ii_status != AC::IMAGE_STATUS_FAILED){
					$ii = new ImageInfrastructureClient();
					$ok = $ii->recoverSource($image->ii_id);
				}else{
					$ok = 1;
				}

				if($ok < 0)
				{
					$this->addError("image:".AC::LOG_OP_RECOVER.":".$this->id, "II Server Error, Recover Image Fails!", json_encode($image));
				}
				else
				{
					$this->addInfo("image:".AC::LOG_OP_RECOVER.":".$this->id, "image recover success", json_encode($image));
				}
			}
			else
			{
				$this->addError("image:".AC::LOG_OP_RECOVER.":".$this->id, "image recover error", json_encode($image));
			}

			$this->setData('type', 'image');
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover image: {$this->id}" . var_export($e,1));

			var_dump($e);
			exit('xxxx');

			return 'system-error';
		}
	}

	///** Merge to unpublish_image
    private function unpublish_premium_image()
    {
        try
		{
			$image = new image();
			$image = $image->get($this->id);
			$log_id = CommonUtils::save_log('image', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($image));
			$unpublish_flag = P_Unpublished::send('image', $this->id, $image->programme_id, $log_id);
			$this->setData('type', 'image');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish image: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }
	// */
	/**
	 * unpublish image
	 */
    private function unpublish_image()
    {
		try
		{
			$image = new image();
			$image = $image->get($this->id);
			$log_id = CommonUtils::save_log('image', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($image));
			Unpublished::send('image', $this->id, $image->programme_id, $log_id);

			// donald 2011-03-21
			//if(CommonUtils::checkLastPublishStatus('image',$this->id,AC::STATUS_PUBLISH_PREMIUM)){
			//	#$log_id = CommonUtils::save_log('image', $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING, json_encode($image));
			//	P_Unpublished::send('image', $this->id, $image->programme_id, $log_id);
			//}

			$this->setData('type', 'image');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish image: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }

	/**
	 * edit image
	 */
    private function edit_image()
    {
		try
		{
			$image = new image();
			$image = $image->get($this->id);
			if ($image->status == AC::STATUS_DELETED)
			{
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}

			$this->setData('id', $this->id);
			$this->setData('type', 'image');
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit image:{$this->id} " . var_export($e,1));

			return 'system-error';
		}
    }

	/**
	 * delete image
	 */
    private function delete_image()
	{
		Model::$dbo['w']->beginTransaction();
		try{
			$image  =   new image();
			$image  =   $image->get($this->id);
			$image->status  =   AC::STATUS_DELETED;
			$image->modified_at =   date("Y-m-d H:i:s");
			$image->modified_by =   CommonUtils::get_user();

			$update_id = Model::$dbo['w']->save($image);

			if ($update_id > 0) {
				//==> by donald
				$image->delete_change_log();

				$image_language = new image_language;
				$image_language_list = $image_language->getAll($this->id);
				foreach ($image_language_list as $image_language_detail) {
					$image_language_detail->delete_change_log();
				}

				$folder_item = new folder_item;
				$folder_item_list = $folder_item->getAll(
					array(
						'type' => 'image', 
						'item_id' => $this->id
					)
				);
				foreach ($folder_item_list as $folder_item) {
					$folder_item->delete_change_log();
				}

				$folder_item_language = new folder_item_language();
				$folder_item_language_list = $folder_item_language->getAllByType('image', $this->id);
				foreach ($folder_item_language_list as $folder_item_language) {
					$folder_item_language->delete_change_log();
				}
				//by donald <==


				// delete folder item and language record
				$item_folders = 'SELECT id FROM folder WHERE folder_item_type_id=:type';
				Model::updateBysql("DELETE FROM `folder_item-language` WHERE item_id=:id AND folder_id IN($item_folders)",array(':type'=>AC::FOLDER_TYPE_IMAGE,':id'=>$this->id));
				Model::updateBysql("DELETE FROM `folder_item` WHERE item_id=:id AND folder_id IN($item_folders)",array(':type'=>AC::FOLDER_TYPE_IMAGE,':id'=>$this->id));


				Model::$dbo['w']->commit();

				// remove remote file
				if($image->ii_status != AC::IMAGE_STATUS_FAILED) {
					$ii = new ImageInfrastructureClient();
					$ok = $ii->DeleteSource($image->ii_id);
				} else {
					$ok = 1;
				}

				if($ok < 0)
				{
					$this->addError("image:".AC::LOG_OP_DELETE.":".$this->id, "II Server Error, Delete Image Fails!", json_encode($image));
				}
				else
				{
					$this->addInfo("image:".AC::LOG_OP_DELETE.":".$this->id, "image delete success", json_encode($image));
				}
			}
			else
			{
				$this->addError("image:".AC::LOG_OP_DELETE.":".$this->id, "image delete error", json_encode($programme));
			}

			$this->setData('type', 'image');

			return  'delete';
		}
		catch(Exception $e)
		{
			var_dump($e);
			exit('xxxxxxxx');
			error_log("delete image: {$this->id}" . var_export($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
    }
}

?>
